package com.hongxun.design.adapters;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.activitys.ChoiceModelActivity;
import com.hongxun.design.activitys.CreateProjectActivity;
import com.hongxun.design.activitys.CreateProjectAttributeActivity;
import com.hongxun.design.activitys.CreateProjectAttributeCodeActivity;
import com.hongxun.design.activitys.ProjectSelfCheckActivity;
import com.hongxun.design.bean.CodeSetVersionBean;
import com.hongxun.design.bean.ProjectItemBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.object_properties.MyMessage;
import com.hongxun.design.ui.dialogstyle.MyDialog;

import java.util.List;

/**
 * Created by zerdoor_pc on 2016/5/19.
 */
public class CreateProjectAttributeCodeSetAdapter extends BaseAdapter {
    List<CodeSetVersionBean> itemBean;
    Context context;
    LayoutInflater inflater;

    public CreateProjectAttributeCodeSetAdapter(List<CodeSetVersionBean> itemBean, Context context) {
        this.itemBean = itemBean;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return itemBean.size();
    }

    @Override
    public Object getItem(int i) {
        return itemBean.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder = null;
        CodeSetVersionBean bean = itemBean.get(i);
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_createattributecodeset, null);
            holder = new Holder(view);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        holder.textView.setText(bean.codeset_version);
        holder.codeset_item.setOnClickListener(new MClickListener(i,bean) {
            @Override
            public void onClick(View view, int position, Object msg) {
                CodeSetVersionBean bean1= (CodeSetVersionBean) msg;
                switch (((CreateProjectAttributeCodeActivity) context).typecode) {
                    case MString.projectChoice://选择项目
                        ProjectItemBean itembean=((CreateProjectAttributeCodeActivity) context).data.get(position);
                        MyApp.ChangeToNewProject(itembean);
                        break;
                    case MString.projectChoice_selfcheck://项目自检——选择项目
                        ProjectItemBean  bean=((CreateProjectAttributeCodeActivity) context).data.get(position);
                        ProjectSelfCheckActivity.setValue(bean);
                        break;
                    case MString.Type_GetPT_ID://典设库
                        ChoiceModelActivity.setValue(Constant.ACTIVITYCODESET,bean1,"1");
                        IntentToActivity.intent(((Activity) context), ChoiceModelActivity.class);
                        break;
                    case  MString.Type_Material:
                        if (MyDialog.dialogUpExpMaterialHandler!=null){
                            MyDialog.dialogUpExpMaterialHandler.sendMessage(MyMessage.getMessage(1, 0,0,bean1.getCodeset_version()));
                            MyDialog.dialogUpExpMaterialHandler.sendMessage(MyMessage.getMessage(2, 0,0,bean1.getCodeset_version_id()));
                            ((Activity) context).finish();
                        }else{
                            ToastShow.showShort(context,"系统错误！");
                        }
                        break;
                    default://典设
                        if (((CreateProjectAttributeCodeActivity) context).code.equals(MString.click_out)){
                            ChoiceModelActivity.setValue(Constant.ACTIVITYCODESET,bean1,CreateProjectAttributeActivity.TYPENUM);
                            IntentToActivity.intent(((Activity) context), ChoiceModelActivity.class);
                        }else{
                            CreateProjectActivity.setValue(Constant.ACTIVITYCODESET,bean1,CreateProjectAttributeActivity.TYPENUM,((CreateProjectAttributeCodeActivity) context).name);
                            IntentToActivity.intent(((Activity) context), CreateProjectActivity.class);
                        }
                        break;
                }

                ((Activity) context).finish();
            }
        });
        return view;
    }

    class Holder {
        TextView textView, right;
        RelativeLayout codeset_item;

        Holder(View view) {
            codeset_item = (RelativeLayout) view.findViewById(R.id.codeset_item);
            textView = (TextView) view.findViewById(R.id.codeset_version);
            right = (TextView) view.findViewById(R.id.codeset_version_right);
            SetTypeface.set(right, IconSourse.RIGHTARROW);
            right.setVisibility(View.GONE);
        }
    }
}
